package CommentTable;

import comment.Comment;
import page.Page;
import utils.JdbcUtils;

import javax.swing.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

public class AddComment {
    public static void init(String name){
        JFrame frame=new JFrame();
        List<Map<String, Object>> datas= JdbcUtils.JdbcQuery("SELECT page_name FROM page");
        String [] options = new String[Page.sum];
        int sum=0;
        for(Map<String,Object> map : datas){
            for(String key : map.keySet()) {
                options[sum++]= (String) map.get(key);
            }
        }
        String info = (String)JOptionPane.showInputDialog(frame,"评论的文章为：","提示",JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
        if(!info.isEmpty()){
            if(Comment.comment_same(info)){
                JOptionPane.showMessageDialog(frame,"要评论的文章不存在");
            }else{
                String info2 = JOptionPane.showInputDialog(frame,"评论内容为:","输入",JOptionPane.WARNING_MESSAGE);
                if(!info2.isEmpty()){
                    LocalDateTime time=LocalDateTime.now();
                    JdbcUtils.JdbcUpdate(Comment.sql_addComment,Comment.sum+1,info2,time,time,0,0,info,name);
                    Comment.jssum();
                    JOptionPane.showMessageDialog(frame,"添加评论成功");
                }
            }
        }
    }
}
